package com.ruoyi.sea.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sea.domain.SeaValueMonth;
import com.ruoyi.sea.domain.SeaValueMonthStatistics;
import com.ruoyi.sea.domain.SeaValueSixMonth;
import com.ruoyi.sea.domain.ValueMonthData;
import com.ruoyi.sea.service.ISeaValueMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 按月统计Controller
 *
 * @author
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/sea/month")
public class SeaValueMonthController extends BaseController {
    @Autowired
    private ISeaValueMonthService seaValueMonthService;

    /**
     * 查询按月统计列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SeaValueMonth seaValueMonth) {
        startPage();
        List<SeaValueMonth> list = seaValueMonthService.selectSeaValueMonthList(seaValueMonth);
        return getDataTable(list);
    }

    @GetMapping("/listByMonth")
    public AjaxResult listByMonth(String code, String oldMonth, String newMonth) {
        List<SeaValueMonth> list = seaValueMonthService.selectSeaValueMonthListByMonth(code, oldMonth, newMonth);
        return AjaxResult.success(list);
    }

    /**
     * 查询按月统计列表
     */
    @GetMapping("/query")
    public AjaxResult queryDetail(SeaValueMonth seaValueMonth) {
        List<SeaValueMonth> list = seaValueMonthService.selectSeaValueMonthList(seaValueMonth);
        if(list != null && list.size() > 0){
            return AjaxResult.success(list.get(0));
        } else {
            return AjaxResult.success();
        }
    }

    @GetMapping("/sixMonthStatistics")
    public AjaxResult queryAvgSeaMonthDaysData(String deviceCode) {
        List<SeaValueSixMonth> list  = seaValueMonthService.getSeaValueSexMonth(deviceCode);
        List<SeaValueMonthStatistics> list1  = new ArrayList<SeaValueMonthStatistics>();
        for (SeaValueSixMonth info : list) {
            SeaValueMonthStatistics statistics = new SeaValueMonthStatistics();
            statistics.setDate(info.getRemark());
            if (StringUtils.isEmpty(info.getCarbonSequestration())) {
                statistics.setValue("0");
            } else {
                statistics.setValue(info.getCarbonSequestration());
            }
            statistics.setType("固碳");
            list1.add(statistics);
        }
        return AjaxResult.success(list1);
    }

    /**
     * 导出按月统计列表
     */
    @Log(title = "按月统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SeaValueMonth seaValueMonth) {
        List<SeaValueMonth> list = seaValueMonthService.selectSeaValueMonthList(seaValueMonth);
        ExcelUtil<SeaValueMonth> util = new ExcelUtil<SeaValueMonth>(SeaValueMonth.class);
        util.exportExcel(response, list, "海洋按月统计数据");
    }

    /**
     * 获取按月统计详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(seaValueMonthService.selectSeaValueMonthById(id));
    }

    /**
     * 新增按月统计
     */
    @Log(title = "按月统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SeaValueMonth seaValueMonth) {
        return toAjax(seaValueMonthService.insertSeaValueMonth(seaValueMonth));
    }

    /**
     * 修改按月统计
     */
    @Log(title = "按月统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SeaValueMonth seaValueMonth) {
        return toAjax(seaValueMonthService.updateSeaValueMonth(seaValueMonth));
    }

    @PutMapping("/updataMonth")
    public AjaxResult editMonth(String code, String month, String arg) {
        return toAjax(seaValueMonthService.updateMonth(code, month, arg));
    }

    /**
     * 删除按月统计
     */
    @Log(title = "按月统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(seaValueMonthService.deleteSeaValueMonthByIds(ids));
    }

    /**
     * 通过类型查询按月统计
     */
    @Log(title = "通过类型查询按月统计", businessType = BusinessType.DELETE)
    @GetMapping("/monthDetail")
    public AjaxResult queryDatasByType(@RequestParam("paramType") String paramType,
                                       @RequestParam("deviceCode") String deviceCode) throws ParseException {
        List<ValueMonthData> list = seaValueMonthService.selectSeaValueMonthListByParams(paramType, deviceCode);
        return AjaxResult.success(list);
    }
}